// @flow
import React from 'react'
import { NavLink } from 'react-router-dom'
import styles from './Nav.css'
import { withRouter } from 'react-router'

import { connect } from 'react-redux'

type Props = {
  userMenu: Object
}
class Nav extends React.Component {
  props: Props
  render () {
    const navList = this.props.userMenu[0] ? this.props.userMenu[0].children : []
    return (
      <ul className={styles['nav']}>
        {
          navList.map((item, index) => {
            if (item.href === '/device') {
              return (
                <li key={index}>
                  <NavLink to='/' activeClassName={styles['link--active']}
                    className={styles['link']} exact isActive={(match, location) => {
                      const { pathname } = location
                      return pathname === '/' ||
                        pathname === '/device-card' ||
                        pathname.indexOf('/device-file/') === 0 ||
                        pathname.indexOf('/fault-distribution') === 0
                    }} >
                    {item.name}
                  </NavLink>
                </li>
              )
            } else {
              return (
                <li key={index}>
                  <NavLink to={item.href} activeClassName={styles['link--active']}
                    className={styles['link']} key={item.id}>
                    {item.name}
                  </NavLink>
                </li>
              )
            }
          })
        }
      </ul>
    )
  }
}

const mapStateToProps = state => {
  return {
    userMenu: state.menu.userMenu
  }
}
const mapDispatchToProps = {
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Nav))
